<template>
	<view class='container'>
		<view class='wrapp'>
			<image src='/static/assets/Slice@2x.png'></image>
		</view>
		<button class='login' open-type="getUserInfo" @getuserinfo="onGotUserInfo">点击登录</button>
	</view>
</template>

<script>
	import api from "../../common/apis.js";
	import message from "../../common/message.js"
	import util from "../../common/util.js"
	let app = getApp()
	export default {
		data() {
			return {
				authCode: "",
				inviteUserId: '',
				type: null,
			};
		},
		onLoad: function(options) {
			if (app.globalData.inviteUserId) {
				this.inviteUserId = app.globalData.inviteUserId
			}
			try {
				uni.clearStorageSync()
			} catch (e) {}
		},
		methods: {
			onGotUserInfo(e) {
				let self=this;
				console.log(e)
				// this.loginAction();
				if (e.detail.errMsg == 'getUserInfo:fail auth deny') {
					uni.showToast({
						title: "请前往小程序设置更改授权项",
						icon: 'none'
					});
					return
				}
				app.globalData.rawData = e.detail.encryptedData;
				app.globalData.signature = e.detail.signature;
				app.globalData.iv = e.detail.iv

				uni.setStorage({
					key: 'rawData',
					data: e.detail.encryptedData
				})
				uni.setStorage({
					key: 'signature',
					data: e.detail.signature
				})

				uni.setStorage({
					key: 'iv',
					data: e.detail.iv
				})

				uni.checkSession({
					success: function() {
						console.log("检查成功")
						self.wxlogin();
					},
					fail: function() {
						console.log("检查失败");
						self.wxlogin();
					}
				})
			},
			wxlogin:function() {
				uni.login({
					success: res => {
						if (res.code) {
							if (res.errMsg == "login:ok") {
								this.authCode = res.code;
								this.loginAction();
							}
						} else {
							console.log("登录失败！" + res.errMsg);
						}
					}
				});
			},
			loginAction:function() {
				let param = {
					code: this.authCode,
					grantType: "wechat_mini_app",
					login: '',
					rawData: app.globalData.rawData,
					signature: app.globalData.signature,
					iv: app.globalData.iv
				};
				uni.showLoading({
					title: "加载中"
				});
				uni.request({
						url: api.loginApi,
						method: "POST",
						data: param,
						header: {
							"content-type": "application/json"
						},
						success: res => {
							uni.hideLoading();
							if (res.statusCode == "200") {
								let token = res.data.id_token;
								try {
									uni.setStorageSync("authorization", token);
								} catch (e) {
									console.log(e);
								}
								uni.switchTab({
									url: '/pages/index/index?bolean=true'
								})
							} else {
								message.showInfo(
									res.data.message ? res.data.message : "网络连接异常"
								);
						}
					}
				});
		}
	}
	}
</script>

<style>
	.wrapp {
		position: fixed;
		top: 0;
		bottom: 0;
		left: 0;
		right: 0;
	}

	.wrapp image {
		width: 100%;
		height: 100%;
	}

	.login {
		position: absolute;
		width: 200px;
		height: 40px;
		line-height: 40px;
		color: white;
		bottom: 200px;
		left: 50%;
		margin-left: -100px;
		background: #366690;
		font-size: 14px;
	}
</style>
